package thebesttaskmanager.inputparser;
import static org.junit.Assert.*;

import java.util.Calendar;
import java.util.Date;

import org.junit.BeforeClass;
import org.junit.Test;



public class InputParserTest {

	InputParser ip = new InputParser();
	Calendar cal = Calendar.getInstance();
	
	@Test
	public void test() {
		Command cmd = ip.getCommand(" add floating task");
		assertEquals("add", cmd.getCmd());;		
		assertEquals("floating task" , cmd.getText());
		
		cmd = ip.getCommand("    addd  200913   test input parser   ");
		assertEquals("add", cmd.getCmd());
		checkDate(cmd, 2013, 8, 20, 0, 0, false);
		assertEquals("test input parser" , cmd.getText());
		
		
		cmd = ip.getCommand(" aDDt 100913 1500  100913        1600 tutorial - by prof ");
		assertEquals("add", cmd.getCmd());
		checkDate(cmd, 2013, 8, 10, 15, 0, false);
		checkDate(cmd, 2013, 8, 10, 16, 0, true);
		assertEquals("tutorial - by prof" , cmd.getText());
	
		
		cmd = ip.getCommand("DELETE  1");
		assertEquals("delete", cmd.getCmd());
		assertEquals("1", cmd.getText());
	
		
		cmd = ip.getCommand("    addd 21/09/13     test input parser   ");
		assertEquals("add", cmd.getCmd());
		checkDate(cmd, 2013, 8, 21, 0, 0, false);
		assertEquals("test input parser" , cmd.getText());

		
		cmd = ip.getCommand(" aDDt 10/09/13 10:00  100913        16:00 d tutorial  ");
		assertEquals("add", cmd.getCmd());
		checkDate(cmd, 2013, 8, 10, 10, 0, false);
		checkDate(cmd, 2013, 8, 10, 16, 0, true);		
		assertEquals("d tutorial" , cmd.getText());
		
		
		cmd = ip.getCommand(" addt 10/09/13 10:02  150913 1522 another timed task  ");
		assertEquals("add", cmd.getCmd());
		checkDate(cmd, 2013, 8, 10, 10, 2, false);
		checkDate(cmd, 2013, 8, 15, 15, 22, true);		
		assertEquals("another timed task" , cmd.getText());
	
		
		cmd = ip.getCommand(" addt 10/09/13   150913  another timed task  ");
		assertEquals("add", cmd.getCmd());
		checkDate(cmd, 2013, 8, 10, 0, 0, false);
		checkDate(cmd, 2013, 8, 15, 0, 0, true);		
		assertEquals("another timed task" , cmd.getText());
		

	}

	private void checkDate(Command cmd, int year, int mth, int day, int hour, int min, boolean getEndDate) {
		Date date = new Date();
		cal.set(Calendar.YEAR , year); cal.set(Calendar.MONTH , mth); cal.set(Calendar.DATE , day);
		cal.set(Calendar.HOUR_OF_DAY , hour); cal.set(Calendar.MINUTE , min); cal.set(Calendar.SECOND , 0);
		cal.set(Calendar.MILLISECOND , 0);
		date = cal.getTime();
				
		if (cmd.isDeadline()) {
			assertEquals(date, cmd.getDeadline());
		} else if (cmd.isTimed() && !getEndDate) {
			assertEquals(date, cmd.getStartDate());
		} else if (cmd.isTimed() && getEndDate) {
			assertEquals(date, cmd.getEndDate());
		}
	}

}
